{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 0
   },
   "source": [
    "# Installation\n",
    ":label:`ch_install`\n",
    "\n",
    "Each section of this book is a Jupyter notebook. The easiest way to run them is clicking the **COLAB** button on the upper right of the HTML page, which will directly you to Google Colab with the corresponding notebook opened. Running the first code cell will connect to a host runtime and show the following warning message. You can click RUN ANYWAY to continue.\n",
    "\n",
    "![Click RUN ANYWAY to run a section on Colab.](../img/colab.png)\n",
    ":width:`400px`\n",
    ":label:`img_colab`\n",
    "\n",
    "The reset of this section will go through how to set up a Python environment, Jupyter's interactive notebooks, the relevant libraries, and the code needed to run the book you can run them on your machines.\n",
    "\n",
    "## Obtaining Source Codes\n",
    "\n",
    "The source code package containing all notebooks is available at\n",
    "http://tvm.d2l.ai/d2l-tvm.zip.\n",
    "Please download it and extract it into a\n",
    "folder. For example, on Linux/macOS, if you have both `wget` and `unzip`\n",
    "installed, you can do it through:\n",
    "\n",
    "```bash\n",
    "wget http://tvm.d2l.ai/d2l-tvm.zip\n",
    "unzip d2l-tvm.zip -d d2l-tvm\n",
    "```\n",
    "\n",
    "## Installing Running Environment\n",
    "\n",
    "If you have both `Python 3.5` or later and `pip` installed, the easiest way to\n",
    "install the running environment is through `pip`. The required packages are\n",
    "\n",
    "- `d2ltvm` for all dependencies such as Jupyter and saved code blocks\n",
    "- `tvm` :cite:`Chen.Moreau.Jiang.ea.2018` for the deep learning compiler we are using\n",
    "- `mxnet` as the baseline in some chapters\n",
    "\n",
    "First install `d2ltvm`:\n",
    "\n",
    "```bash\n",
    "pip install git+https://github.com/d2l-ai/d2l-tvm\n",
    "```\n",
    "\n",
    "Then compile `tvm` from source codes. TVM doesn't have a pip package because it\n",
    "highly depends on the libraries available on your system. Please follow the\n",
    "instructions  on\n",
    "[tvm.ai](https://docs.tvm.ai/install/from_source.html) to install `tvm`. The configuration in `config.cmake` this\n",
    "book requires at least\n",
    "\n",
    "```bash\n",
    "set(USE_LLVM ON)\n",
    "```\n",
    "\n",
    "If you plan to run on Nvidia GPUs as well, you will also need to\n",
    "\n",
    "```bash\n",
    "set(USE_CUDA ON)\n",
    "```\n",
    "\n",
    "Also\n",
    "don't forget to enable `cython`, which accelerates the performance. You just\n",
    "need to run `make cython` in the TVM source folder.\n",
    "\n",
    "If luckily you are using Ubuntu with `python-3.7`, `llvm-6.0` and `cuda-10.1` installed, you\n",
    "may use the pre-built library that is for evaluating this book:\n",
    "\n",
    "```bash\n",
    "pip install https://tvm-repo.s3-us-west-2.amazonaws.com/tvm-0.7.dev1-cp37-cp37m-linux_x86_64.whl\n",
    "```\n",
    "\n",
    "Our code runs on tvm-0.7-dev1 for now.\n",
    "\n",
    "Finally, install MXNet's CUDA version if GPUs are available :cite:`Chen.Li.Li.ea.2015`. Assume you are have\n",
    "CUDA 10.1 installed, then\n",
    "\n",
    "```bash\n",
    "pip install mxnet-cu101\n",
    "```\n",
    "\n",
    "You can change the `101` to match your CUDA version.\n",
    "\n",
    "Once all packages are installed, you can open the Jupyter notebook by\n",
    "\n",
    "```bash\n",
    "jupyter notebook\n",
    "```\n",
    "\n",
    "At this point open http://localhost:8888 (which usually opens automatically) in the browser, then you can view and run the code in each section of the book.\n",
    "\n",
    "\n",
    "## Code\n",
    ":label:`ch_code`\n",
    "\n",
    "Throughout the book, we save reusable code blocks in the `d2ltvm` package by adding the comment: \"`# Save to the\n",
    "d2ltvm package.`\" before the code block. For example, the following code snippet shows the\n",
    "libraries imported by `d2ltvm`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "1"
    },
    "origin_pos": 1,
    "tab": [
     "tvm"
    ]
   },
   "outputs": [],
   "source": [
    "# Save to the d2ltvm package.\n",
    "import tvm\n",
    "from tvm import te\n",
    "import time\n",
    "import timeit\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from IPython import display\n",
    "try:\n",
    "  import mxnet as mx\n",
    "except:\n",
    "  pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 2
   },
   "source": [
    "## [Discussions](https://discuss.tvm.ai/t/getting-started-installation/4706)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}